home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / lib / mathlib / libconv / EXAMPLES / cex2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  1.4 KB  |  45 lines

  1. /*
  2. * This program illustrates relation between CONVOLUTION and CORRELATION
  3. *
  4. * Correlation is equivalent to convolution with Flipped array
  5. */
  6. #include <stdio.h>
  7. #include <math.h>
  8. #include "conv.h"
  9.  
  10. void float_seq_print( float *f_array, int n_pts);
  11. #define NPTS 8
  12.  
  13. main() {
  14.     int i;
  15.     float seq1[NPTS], seq2[NPTS], cor1[NPTS], cor2[NPTS];
  16.  
  17. /* Initialize Sequences ... seq2 is seq1 shifted by two samples */
  18.     seq1[0] = seq1[1] = seq2[NPTS-2] = seq2[NPTS-1] = 0.;
  19.     for (i = 2; i < NPTS ; i++)
  20.         seq1[i] = seq2[i-2] = 10. * pow( -.3, (double)i);
  21.  
  22. /* Correlation */
  23.     scor1d( seq1,1,0,NPTS,seq2,1,0,NPTS,cor1,1,0,NPTS);
  24.  
  25. /* Convolution with flipped array */
  26. /* We actually just call sfir1d with the flipping parameters */
  27. /* New origin of array is last sample, new increment is -(old increment) */
  28.     sfir1d( seq1,1,0,NPTS,seq2+(NPTS-1),-1,-(NPTS-1),NPTS,cor2,1,0,NPTS,1.,0.);
  29.  
  30. /* Print out the Input sequences and the Correlation functions */
  31.     printf("Seq # 1 : \n");
  32.       float_seq_print( seq1, NPTS);
  33.     printf("Seq # 2 : \n");
  34.       float_seq_print( seq2, NPTS);
  35.     printf("Correlation using \"scor1d\" : \n");
  36.       float_seq_print( cor1, NPTS);
  37.     printf("Correlation using \"sfir1d\" : \n");
  38.       float_seq_print( cor2, NPTS);
  39. }
  40. void float_seq_print( float *f_array, int n_pts){
  41.     for ( ; n_pts > 0; n_pts--)
  42.     printf("%9.5f", *f_array++);
  43.     printf("\n");
  44. }
  45.